import 'dart:math';

import 'package:doodle_dash/tank/MyTank.dart';
import 'package:doodle_dash/tank/component/BulletTank.dart';
import 'package:flame/extensions.dart';

import 'Tank.dart';


class EnemyTank extends TankModel {
  EnemyTank(MyTank game, String url1, String url2, Offset offset, TankID tankId)
      : super(game, url1, url2, offset, tankId) {
    bodyRect =
        Rect.fromLTWH(-20 * ration, -15 * ration, 38 * ration, 32 * ration);
    paoRect = Rect.fromLTWH(-1, -2 * ration, 22 * ration, 6 * ration);
    initOffset();
  }

  void initOffset() {
    double x =
        Random().nextDouble() * (game.screenSize.width - (seedNum * seedRatio));
    double y = Random().nextDouble() *
        (game.screenSize.height - (seedNum * seedRatio));
    targetOffset = Offset(x, y);

    Offset temp = targetOffset - position;
    bodyAngleT = temp.direction;
    //paoAngleT = temp.direction;
  }

  double enemyTime = 0;

  onEnemyFire() {
    final bullet = BulletTank(
        speed: 300,
        offset: position,
        myAngle: bodyAngle,
        id: tankId,
        screenSize: game.canvasSize);
    game.enemyBullets.add(bullet);
  }

  @override
  void update(double dt) {
    super.update(dt);
    enemyTime += dt;
    if (enemyTime >= 3) {
      //onEnemyFire();
      enemyTime = 0;
    }
  }

  void move(double dt) {
    if (bodyAngleT != 0) {
      if (targetOffset != 0) {
        moveDis += speed * dt;
        if (moveDis < 100) {
          if (bodyAngle == bodyAngleT) {
            //tank 直线时 移动速度快
            position = position +
                Offset.fromDirection(bodyAngle, speedEnemy * dt); //100 是像素
          } else {
            //tank旋转时 移动速度要慢
            position =
                position + Offset.fromDirection(bodyAngle, turnSpeedEnemy * dt);
          }
        } else {
          moveDis = 0;
          initOffset();
        }
      }
    }
  }

  @override
  double getBulletAngle() {
    // TODO: implement getBulletAngle
    throw UnimplementedError();
  }

  @override
  Offset getBulletOffset() {
    // TODO: implement getBulletOffset
    throw UnimplementedError();
  }
}
